<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adhAdvertName">
              <a-input v-model="model.adhAdvertName" placeholder="请输入广告标题"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
         <a-row>
          <a-col :span="24">
            <a-form-model-item label="链接URL" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adhAdvertUrl">
              <a-input v-model="model.adhAdvertUrl" placeholder="请输入广告URL"  ></a-input>
            </a-form-model-item>
          </a-col>
          </a-row>
            <a-row>
           <a-col :span="24">
            <a-form-model-item label="广告位排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adhAdvertSort">
              <a-input-number v-model="model.adhAdvertSort" placeholder="请输入广告排序(越小优先级越高)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          </a-row>
          <a-row>
          <a-col :span="24">
            <a-form-model-item label="广告类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adhAdvertType">
              <a-radio-group default-value="1"  v-model="model.adhAdvertType" button-style="solid">
                  <a-radio-button value="1">
                    站内
                  </a-radio-button>
                  <a-radio-button value="2">
                    站外
                  </a-radio-button>
                </a-radio-group>
            </a-form-model-item>
          </a-col>
           </a-row>
          <a-row>
          <a-col :span="24">
            <a-form-model-item label="广告状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adhAdvertStatus">
                <a-radio-group default-value="1"  v-model="model.adhAdvertStatus" button-style="solid">
                  <a-radio-button value="1">
                    启用
                  </a-radio-button>
                  <a-radio-button value="2">
                    停用
                  </a-radio-button>
                </a-radio-group>
                </a-form-model-item>
          </a-col>
           </a-row>
          
          <a-row>
          <a-col :span="24">
            <a-form-model-item label="建议图片大小(720:302)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="adhAdvertImg">
                <j-image-upload class="avatar-uploader" text="上传" v-model="model.adhAdvertImg" ></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
function getBase64(img, callback) {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result));
  reader.readAsDataURL(img);
}
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AdhAdvertForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          adhAdvertName:[{required: true, message: '请输入广告位标题!',maxlength:10,}],
          adhAdvertUrl:[{required: true, message: '请输入广告的链接URL信息!',maxlength:300,}],
          adhAdvertImg:[{required: true, message: '请上传广告位图!'}],
        },
        url: {
          add: "/advert/adhAdvert/add",
          edit: "/advert/adhAdvert/edit",
          queryById: "/advert/adhAdvert/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'post';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>
<style>
.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>